#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.8.1
 Author:         myName

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------

Global $shop_item_level[10]
Global $shop_item_type[5] = [ _
   0x78B4FF,  _ ; magic
   0xA3D940, _  ; good
   0xB67AFF, _  ; rare
   0xF75D42, _  ; perfect
   0xFFA12B _   ; unique
]
Global Const $shop_item_point1 = 80
Global Const $shop_item_point2 = 220
Global Const $item_color_limit = 200
Global $ocr_data_need = True

Global $cnt_dbg = 1000000

Func getItemLevel(ByRef $array,$width,$height)
	;_DebugOut("getItemLevel(" & $width & "," & $height & ")")
	If $ocr_data_need Then
		ocr_load_data("digit")
		$ocr_data_need = False
	EndIf
	Local $m = get_level_box($array,$width,$height)
	Local $found = ocr_search_data($m)
	If $found < 0 Then
		_ScreenCapture_SaveImage("dump-" & $cnt_dbg & ".jpg",$item_info_hBMP,False)
		_DebugOut("stored : " & $cnt_dbg)
		_DebugOut(save_matrix($m))
	EndIf
	If $found = 0 Then
		$found = 10
	EndIf
	Return $found
EndFunc

Func get_level_box(ByRef $array,$width,$height)
	Local $sy
	Local $has = False
	Local $color
	For $y = 0 To $height
		Local $c = getPixel($array,$width,$shop_item_point1,$y)
		For $i = 0 To 4
			;_DebugOut("get_level_box: check color 0x" & Hex($c,6) & " with 0x" & Hex($shop_item_type[$i],6))
			If $c = $shop_item_type[$i] Then
				If getPixel($array,$width,$shop_item_point2,$y) = $c Then
					$sy = $y
					$color = $shop_item_type[$i]
					ExitLoop 2
				EndIf
			EndIf
		Next
	Next
	;_DebugOut("get_level_box: found color sy=" & $sy & " color= 0x" & Hex($color,6))
	For $y = $sy To $height
		If getPixel($array,$width,$shop_item_point1,$y) <> $color Or getPixel($array,$width,$shop_item_point2,$y) <> $color Then
			$sy = $y
			ExitLoop
		EndIf
	Next
	;_DebugOut("get_level_box: color ended sy=" & $sy)
	Local $rb = Floor($width/2)
	Local $a = getFilledLine($array,$width,10,$sy+1,$rb,$height) ;find first line
	;_DebugOut("get_level_box: filled line x=" & $a[0] & " y=" & $a[1])
	$sy = getEmptyLine($array,$width,$a[0],$a[1]+1,$rb,$height)  ;find empty line
	;_DebugOut("get_level_box: empty line sy=" & $sy)
	$a = getFilledLine($array,$width,$a[0],$sy+1,$rb,$height) ;find level line
	;_DebugOut("get_level_box: filled line x=" & $a[0] & " y=" & $a[1])
	Local $b = getFilledRow($array,$width,$a[0] + 50, $a[1], $width, $a[1] + 15); Find digit
	;_DebugOut("get_level_box: filled row x=" & $b[0] & " y=" & $b[1])
	Local $m = getMatrix($array,$width,$b[0], $a[1], $b[0] + 13, $a[1] + 15) ; ask digit
	Return $m
EndFunc

Func getMatrix(ByRef $array,$width,$l,$t,$r,$b)
	;_DebugOut("getMatrix(" & $l & "," & $t & "," & $r & "," & $b & ")")
	Local $m[$b-$t+1][$r-$l+1]
	Local $last_y = $b + 1
	Local $w = $r - $l + 1
	For $y = $t To $b
		Local $empty = True
		For $x = $l To $r
			If getPixelLight($array,$width,$x,$y) < $item_color_limit Then
				$m[$y-$t][$x-$l] = 1
				$empty = False
			Else
				$m[$y-$t][$x-$l] = 0
			EndIf
		Next
		If $empty Then
			If $last_y > $y Then
				$last_y = $y
			EndIf
		Else
			$last_y = $b + 1
		EndIf
	Next

	Local $last_x = $r + 1
	For $x = $l To $r
		Local $empty = True
		For $y = $t To $last_y - 1
			If $m[$y-$t][$x-$l] Then
				$empty = False
			EndIf
		Next
		If $empty Then
			If $last_x > $x Then
				;_DebugOut("new last_x: " & $x)
				$last_x = $x
			EndIf
		Else
			$last_x = $r + 1
		EndIf
	Next
	;_DebugOut("last_x: " & $last_x & ", last_y: " & $last_y)

	Local $rm[$last_y-$t][$last_x-$l]
	For $x = $l To $last_x - 1
		For $y = $t To $last_y - 1
			$rm[$y-$t][$x-$l] = $m[$y-$t][$x-$l]
		Next
	Next
	Return $rm
EndFunc

Func getFilledRow(ByRef $array,$width,$l,$t,$r,$b)
	;_DebugOut("getFilledRow(" & $l & "," & $t & "," & $r & "," & $b & ")")
	Local $w = $r - $l + 1
	For $x = $l To $r
		For $y = $t To $b
			If getPixelLight($array,$width,$x,$y) < $item_color_limit Then
				Local $a[2] = [$x,$y]
				Return $a
			EndIf
		Next
	Next
	Local $a[2] = [$r,$b]
	Return $a
EndFunc

Func getEmptyLine(ByRef $array,$width,$l,$t,$r,$b)
	;_DebugOut("getEmptyLine(" & $l & "," & $t & "," & $r & "," & $b & ")")
	Local $w = $r - $l + 1
	For $y = $t To $b
		Local $empty = True
		For $x = $l To $r
			If getPixelLight($array,$width,$x,$y) < $item_color_limit Then
				$empty = False
				ExitLoop
			EndIf
		Next
		If $empty Then
			Return $y
		EndIf
	Next
	Return $b
EndFunc

Func getFilledLine(ByRef $array,$width,$l,$t,$r,$b)
	;_DebugOut("getFilledLine(" & $l & "," & $t & "," & $r & "," & $b & ")")
	Local $w = $r - $l + 1
	For $y = $t To $b
		For $x = $l To $r
			If getPixelLight($array,$width,$x,$y) < $item_color_limit Then
				Local $a[2] = [$x,$y]
				Return $a
			EndIf
		Next
	Next
	Local $a[2] = [$l,$b]
	Return $a
EndFunc

Func ocr_load_data($dir)
	Local $search = FileFindFirstFile($dir & "\*.txt")
	If $search = -1 Then
		_DebugOut("no files")
		Return
	EndIf
	While True
		Local $file = FileFindNextFile($search)
		If @error Then ExitLoop
		ocr_load_data_file($dir,$file)
	WEnd
	FileClose($search)
EndFunc

Func ocr_load_data_file($dir,$file)
	Local $fh = FileOpen($dir & "\" & $file)
	Local $ret2 = StringRegExp($file,"(\d+)",1)
	Local $number
	If @error = 0 Then
		$number = $ret2[0]
	Else
		_DebugOut("bad " & $file)
		Return
	EndIf
	If $number = 10 Then
		$number = 0
	EndIf
	Local $ary[50][50], $max_x = 0, $max_y = 0
	While True
		Local $line = FileReadLine($fh)
		If @error = -1 Then ExitLoop
		Local $ret = StringRegExp($line,"(\d)",3)
		If @error <> 0 Then
			_DebugOut("bad file: " & $file & " line: " & $line)
		EndIf
		$max_x = UBound($ret) - 1
		For $x = 0 To $max_x
			$ary[$max_y][$x] = $ret[$x]
		Next
		$max_y += 1
	WEnd
	$max_y -= 1
	FileClose($fh)
	Local $m[$max_y + 1][$max_x + 1]
	For $y = 0 To $max_y
		For $x = 0 To $max_x
			$m[$y][$x] = $ary[$y][$x]
		Next
	Next
	$shop_item_level[$number] = $m
EndFunc

Func ocr_search_data(ByRef $m)
	Local $xm = UBound($m,2) - 1
	Local $ym = UBound($m,1) - 1
	For $i = 0 To 9
		Local $d = $shop_item_level[$i]
		Local $my = UBound($d,1) - 1
		Local $has = True
		If $my <> $ym Then
			ContinueLoop
		EndIf
		For $y = 0 To $my
			Local $mx = UBound($d,2) - 1
			If $mx <> $xm Then
				$has = False
				ContinueLoop 2
			EndIf
			For $x = 0 To $mx
				If $m[$y][$x] <> $d[$y][$x] Then
					$has = False
					ExitLoop 2
				EndIf
			Next
		Next
		If $has Then
			Return $i
		EndIf
	Next
	Return -1
EndFunc

Func save_matrix(ByRef $m)
	Local $str = ""
	For $y = 0 To Ubound($m,1)-1
		For $x = 0 To Ubound($m,2)-1
			$str &= $m[$y][$x]
		Next
		$str &= @CRLF
	Next
	Return $str
EndFunc


